package mk.company.fin.model;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
//import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import mk.company.fin.FinConstants;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Index;

@Entity
@Table(name = FinConstants.Tables.KONTO, 
		uniqueConstraints = {@UniqueConstraint(columnNames = {"value", "id_konto_synthetic"})})
public class Konto extends Model {

	private static final long serialVersionUID = -2911881094017253394L;

	// persistent
	private Long id;
	private String value;
	private String description;
	private KontoSynthetic kontoSynthetic;
	private List<NalogEntry> nalogEntries;
	
	// dynamic
	private String konto;
	
	
	@Id
	@Column(name = "id_konto_analytic")
	@GeneratedValue//(strategy = GenerationType.SEQUENCE)
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}

	@Column(nullable = false, length = 10)
	@Index(name = FinConstants.Tables.KONTO + "_search")
	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		if (StringUtils.isNotEmpty(value) && value.length() <= 10) {
			this.value = value;
		}
	}

	@Column
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@ManyToOne(cascade = {CascadeType.ALL,CascadeType.DETACH}, fetch = FetchType.EAGER)
	@JoinColumn(name = "id_konto_synthetic", nullable = false)
	@Index(name = FinConstants.Tables.KONTO + "_search")
	public KontoSynthetic getKontoSynthetic() {
		return kontoSynthetic;
	}

	public void setKontoSynthetic(KontoSynthetic kontoSynthetic) {
		this.kontoSynthetic = kontoSynthetic;
	}
	
	@OneToMany(mappedBy = "konto", orphanRemoval = false, fetch = FetchType.LAZY)
	public List<NalogEntry> getNalogEntries() {
		return nalogEntries;
	}

	public void setNalogEntries(List<NalogEntry> nalogEntries) {
		this.nalogEntries = nalogEntries;
	}
	
	
	/**
	 * @return Class + Group + Synthetic + Analytic = Konto
	 */
	@Transient
	public String getKonto() {
		if (konto == null) {
			KontoSynthetic kontoSynthetic = getKontoSynthetic();
			if (kontoSynthetic != null) {
				konto = kontoSynthetic.getSynthetic() + getValue();
			} else {
				konto = "???" + getValue();
			}
		}
		return konto;
	}
	
	@Deprecated
	public void setKonto(String konto) {
		throw new UnsupportedOperationException("The field konto is dynamic!");
	}
	
	@Override
	public String toString() {
		return getKonto();
	}
	
	public void removeEntry(NalogEntry nalogEntry){
		nalogEntries.remove(nalogEntry);
		
	}
	
}
